package com.ssmTest.service.impl;


import com.ssmTest.dao.IUserInformationDao;
import com.ssmTest.model.Order;
import com.ssmTest.model.UserInformation;
import com.ssmTest.service.DateTimeService;
import com.ssmTest.service.IOrderService;
import com.ssmTest.service.IUserInformationService;
import com.ssmTest.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("userInformationService")
public class UserInformationServiceImpl implements IUserInformationService {
    @Resource
    IUserInformationDao iUserInformationDao;
    @Autowired
    IUserInformationService iUserInformationService;
    @Autowired
    IUserService iUserService;
    @Autowired
    DateTimeService dateTimeService;
    @Autowired
    IOrderService iOrderService;

    //根据id查找订单
    public UserInformation selectUserInformation(int id) {
        return this.iUserInformationDao.selectUserInformation(id);
    }

    //插入订单
    public void insertUserInformation(UserInformation userInformation) {
        this.iUserInformationDao.insertUserInformation(userInformation);
    }

    //未审核订单列表
    public List<UserInformation> InformationList(int te) {
        List<UserInformation> list = iUserInformationDao.informationList(te);
        return list;
    }

    //更新Information
    public void updateInformationReasion(UserInformation userInformation) {
        this.iUserInformationDao.updateInformation(userInformation);
    }

    //关键字查询
    public List<UserInformation> selectUserInformationByAndName(int pagenum, String city, String name) {
        List<UserInformation> list = iUserInformationDao.selectUserInformationByAndName((pagenum - 1) * 10, pagenum * 10, city, name);
        return list;
    }

    public List<UserInformation> selectUserInformationByNoName(int pagenum, String city) {
        List<UserInformation> list = iUserInformationDao.selectUserInformationByNoName((pagenum - 1) * 10, pagenum * 10, city);
        return list;
    }

    //订购信息
    public int orderUserInformation(int id, int first_party, int second_party) {
        //-1该信息不存在 -2已被下单 -3订单状态异常 其它正数，订单id
        UserInformation userInformation = iUserInformationService.selectUserInformation(id);
        if (userInformation == null)
            return -1;
        if (userInformation.getToexamine() == 3)
            return -2;
        if (userInformation.getToexamine() != 1)
            return -3;
        Order order = new Order();
        order.setFirst_party(first_party);
        order.setSecond_party(second_party);
        order.setTime(dateTimeService.getTime());
        order.setInformation(id);
        order.setToexamine(0);
        order.setType(1);
        order.setTitle(userInformation.getTitle());


        userInformation.setToexamine(3);
        iUserInformationService.updateInformationReasion(userInformation);


        return iOrderService.insertOrder(order);
    }

    public int numberOfInformation(int toexamine) {
        return this.iUserInformationDao.numberOfInformation(toexamine);
    }

    public List<UserInformation> selectByPersionalId(int personal_id) {
        return this.iUserInformationDao.selectByPersionalId(personal_id);
    }
}
